﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DoubleH.Utility;
using Table = FCNS.Data.Table;
using FCNS.Data;
using System.Data;
using System.Diagnostics;
using System.Collections;
namespace ReportS
{
    /// <summary>
    /// 商品出入库明细表.xaml 的交互逻辑
    /// </summary>
    public partial class 商品出入库明细表 : Window
    {
        public 商品出入库明细表()
        {
            InitializeComponent();
            if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
                return;

            InitVar();
            InitEvent();
        }

        IList productS = null;
        public void InitVar()
        {
            uCdataGrid1.TableText = DataTableText.商品出入库明细表;
            uCdataGrid1.checkBoxChart.IsChecked = false;
            uCdataGrid1.checkBoxChart.IsEnabled = false;
            uCdataGrid1.checkBoxDetail.IsEnabled = false;

            checkComboBoxStoreS.DisplayMemberPath = "Name";
            checkComboBoxStoreS.ValueMemberPath = "Id";
            checkComboBoxStoreS.ItemsSource = MainWindow.allStoreS;

            checkComboBoxGroupS.DisplayMemberPath = "Name";
            checkComboBoxGroupS.ValueMemberPath = "Id";
            checkComboBoxGroupS.ItemsSource = Table.GroupS.GetList(Table.GroupS.EnumFlag.商品分类, Table.GroupS.EnumEnable.启用);
        }

        private void InitEvent()
        {
            buttonProductS.Click += (ss, ee) =>
                {
                    ProductS.GetProductS gp = new ProductS.GetProductS();
                    gp.Init(ProductS.GetProductS.EnumProductS.询价单商品, null);
                        gp.ShowDialog();
                    productS=gp.Selected;
                };

            buttonOK.Click += (ss, ee) => FillData();
        }

        private void FillData()
        {
            StringBuilder sb = new StringBuilder("SELECT  StoreS.Name AS StoreSName, ProductS.Name AS ProductSName, ProductSIO.OrderNO, ProductSIO.ProductSId, ProductSIO.PurchasePrice, ProductSIO.WholesalePrice, ProductSIO.AveragePrice, ProductSIO.Quantity, ProductSIO.OrderDateTime, ProductSIO.StoreSId");
            sb.Append(" FROM ProductSIO INNER JOIN  ProductS ON ProductSIO.ProductSId = ProductS.Id INNER JOIN StoreS ON ProductSIO.StoreSId = StoreS.Id");
            sb.Append(" where cast(ProductSIO.OrderDateTime as varchar)>='" + uCDateTime1.StartDateTime.ToString(FCNS.Data.DbDefine.dateTimeFormat) + "'");
            sb.Append(" and cast(ProductSIO.OrderDateTime as varchar)<='" + uCDateTime1.EndDateTime.ToString(FCNS.Data.DbDefine.dateTimeFormat) + "'");
            sb.Append(" and ProductSIO.Enable=1");

            if (!string.IsNullOrEmpty(checkComboBoxGroupS.Text))
            {
                sb.Append(" and ProductSIO.ProductSId in (select Id from ProductS where ");
                foreach (string str in checkComboBoxGroupS.SelectedValue.Split(','))
                    sb.Append("GroupSId=" + str + " or ");

                sb.Remove(sb.Length - 3, 3);
                sb.Append(")");
            }
            if (!string.IsNullOrEmpty(checkComboBoxStoreS.Text))
            {
                sb.Append(" and (");
                foreach (string str in checkComboBoxStoreS.SelectedValue.Split(','))
                    sb.Append("ProductSIO.StoreSId=" + str + " or ");

                sb.Remove(sb.Length - 3, 3);
                sb.Append(")");
            }
            if (productS != null && productS.Count != 0)
            {
                sb.Append(" and (");
                foreach (Table.ProductS ps in productS)
                    sb.Append("ProductSIO.ProductSId=" + ps.Id + " or ");

                sb.Remove(sb.Length - 3, 3);
                sb.Append(")");
            }
            productS = null;


            List<TempFunction> temp = new List<TempFunction>();
            foreach (DataRow row in SQLdata.GetDataRows(sb.ToString()))
            {
                if (row == null)
                    continue;

                TempFunction tf = new TempFunction()
                {
                    ProductSName = row["ProductSName"] as string,
                    PurchasePrice = (double)row["PurchasePrice"],
                    WholesalePrice = (double)row["WholesalePrice"],
                    Quantity = (double)row["Quantity"],
                    AveragePrice = (double)row["AveragePrice"],
                    OrderDateTime = DateTime.ParseExact(row["OrderDateTime"] as string, DbDefine.dateTimeFormat, null),
                    StoreSName = row["StoreSName"] as string
                };

                string orderNO = row["OrderNO"] as string;
                if (orderNO.StartsWith("CG"))
                {
                    tf.WholesalePrice = null;
                    if (orderNO.Contains("DD"))
                        tf.OrderNO = "采购订单-" + orderNO;
                    else if (orderNO.Contains("TH"))
                        tf.OrderNO = "采购退货-" + orderNO;
                    else if (orderNO.Contains("ZP"))
                        tf.OrderNO = "采购赠品-" + orderNO;
                }
                else if (orderNO.StartsWith("PF"))
                {
                    tf.PurchasePrice = null;
                    if (orderNO.Contains("DD"))
                        tf.OrderNO = "批发订单-" + orderNO;
                    else if (orderNO.Contains("TH"))
                        tf.OrderNO = "批发退货-" + orderNO;
                    else if (orderNO.Contains("ZP"))
                        tf.OrderNO = "批发赠品-" + orderNO;
                }
                else if (orderNO.StartsWith("CK") && orderNO.Contains("PD"))
                {
                    if (tf.Quantity > 0)
                        tf.OrderNO = "盘盈-" + orderNO;
                    else
                        tf.OrderNO = "盘亏-" + orderNO;
                }
                else if (orderNO.StartsWith("POS"))
                {
                    tf.PurchasePrice = null;
                    tf.OrderNO = "零售单-" + orderNO;
                }
                temp.Add(tf);
            }
            uCdataGrid1.Init(temp);
        }


        internal class TempFunction
        {
            public string OrderNO { get; set; }
            public string ProductSName { get; set; }
            public double? PurchasePrice { get; set; }
            public double? WholesalePrice { get; set; }
            public double Quantity { get; set; }
            public double AveragePrice { get; set; }
            public DateTime OrderDateTime { get; set; }
            public string StoreSName { get; set; }
        }
    }
}